package cn.stylefeng.guns.modular.rms.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.rms.entity.Company;
import cn.stylefeng.guns.modular.rms.entity.CompanyPosition;
import cn.stylefeng.guns.modular.rms.mapper.CompanyMapper;
import cn.stylefeng.guns.modular.rms.mapper.CompanyPositionMapper;
import cn.stylefeng.guns.modular.rms.model.params.CompanyParam;
import cn.stylefeng.guns.modular.rms.model.params.CompanyPositionParam;
import cn.stylefeng.guns.modular.rms.model.result.CompanyPositionResult;
import cn.stylefeng.guns.modular.rms.model.result.CompanyResult;
import cn.stylefeng.guns.modular.rms.service.CompanyPositionService;
import cn.stylefeng.guns.utils.json.ValidatorUtils;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 职位信息表 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2021-03-23
 */
@Service
public class CompanyPositionServiceImpl extends ServiceImpl<CompanyPositionMapper, CompanyPosition> implements CompanyPositionService {

    @Resource
    private CompanyMapper companyMapper;

    @Override
    public void add(CompanyPositionParam param) {
        CompanyPosition entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CompanyPositionParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(CompanyPositionParam param) {
        if (ValidatorUtils.notEmpty(param.getSalary())) {
            String[] attr = param.getSalary().split("-");
            if (attr.length == 2) {
                param.setSalaryLow(attr[0]);
            }
        }
        //判断ID是否存在，如果不存在，则新建
        if (param.getId() != null) {
            CompanyPosition oldEntity = getOldEntity(param);
            CompanyPosition newEntity = getEntity(param);
            ToolUtil.copyProperties(newEntity, oldEntity);
            this.updateById(newEntity);
        } else {
            this.add(param);
        }
    }

    @Override
    public CompanyPositionResult findBySpec(CompanyPositionParam param) {
        return this.baseMapper.findBySpec(param);
    }

    @Override
    public List<CompanyPositionResult> findListBySpec(CompanyPositionParam param) {
        return baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(CompanyPositionParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findPageBySpecForCom(CompanyPositionParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageListForCom(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findPageBySpecForAdmin(CompanyPositionParam param) {
        Page pageContext = getPageContext();
        IPage<CompanyPositionResult> page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(CompanyPositionParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private CompanyPosition getOldEntity(CompanyPositionParam param) {
        return this.getById(getKey(param));
    }

    private CompanyPosition getEntity(CompanyPositionParam param) {
        CompanyPosition entity = new CompanyPosition();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
